package com.globallogic.javatrainings.sergiiivanov.controller;

import com.globallogic.javatrainings.sergiiivanov.model.Teacher;
import com.globallogic.javatrainings.sergiiivanov.model.University;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityFailure;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;
import com.globallogic.javatrainings.sergiiivanov.resources.Messages;
import com.globallogic.javatrainings.sergiiivanov.view.TeachersManagementPage;

class TeachersMenuController extends BasicController {
    private enum States {
        SHOW_TEACHERS_LIST, SHOW_TEACHER_DETAILS
    }

    private University university;
    private TeachersManagementPage page = new TeachersManagementPage();
    private States currentState;
    private int command;
    private Teacher teacher;

    @Override
    BasicController initiate(University u) throws UniversityFailure {
        university = u;
        if (university == null) {
            throw new UniversityFailure("University is not selected");
        }

        if (currentState == States.SHOW_TEACHERS_LIST) {
            return showTeachersList();
        }
        if (currentState == States.SHOW_TEACHER_DETAILS) {
            return showTeacherDetails();
        }

        currentState = States.SHOW_TEACHERS_LIST;
        return this;
    }

    private BasicController addNewTeacher() throws UniversityFailure {
        page.pageAddNewTeacher();
        String[] form = { "Enter name: ", "Enter phone: ", "Enter email: " };
        String[] data = questionnaire(form);
        try {
            university.teachers.addPerson(data[0], data[1], data[2]);
        } catch (UniversityWarning e) {
            page.error("Failed to add teacher", e.getMessage());
        }
        try {
            teacher = university.teachers.getTeacherByIndex(university.teachers.countTeachers());
        } catch (UniversityWarning e) {
            page.error("Failed to get new added teacher", e.getMessage());
        }
        currentState = States.SHOW_TEACHERS_LIST;
        return this;
    }

    private BasicController addResumeEntry() {
        if (teacherNotSelected())
            return this;
        String[] questions = { "Year: ", "Summary: ", "Details: " };
        String[] answers = questionnaire(questions);
        try {
            teacher.addResumeEntry(answers);
        } catch (UniversityWarning e) {
            page.error("Failed to add resume entry", e.getMessage());
        }
        return this;
    }

    private BasicController deleteResumeEntry(int indexResumeEntry) throws UniversityFailure {
        if (teacherNotSelected())
            return this;
        page.pageConfirmResumeEntryDelete();
        if (yes()) {
            try {
                teacher.removeResumeEntry(indexResumeEntry);
            } catch (UniversityWarning e) {
                page.error("Failed to remove resume entry", e.getMessage());
            }
        }
        return this;
    }

    private BasicController deleteTeacher() {
        if (teacherNotSelected())
            return this;
        page.pageConfirmDelete();
        if (yes()) {

            university.teachers.removeTeacher(teacher);
            teacher = null;
            currentState = States.SHOW_TEACHERS_LIST;
        }
        return this;
    }

    private void readCommand() {
        command = InputController.readInt();
    }

    private BasicController showCourses() throws UniversityFailure {
        if (teacherNotSelected())
            return this;
        String[] courses = teacher.getCourses().toNamesList();
        page.pageShowCourses(teacher.getName(), courses);
        readCommand();
        currentState = States.SHOW_TEACHER_DETAILS;
        if (command == courses.length + 1) {
            return showScores();
        }
        return this;
    }

    private BasicController showScores() {
        page.pageScores(teacher.getName(), teacher.getScoreTable());
        readCommand();
        return this;
    }

    private BasicController showResume() throws UniversityFailure {
        if (teacherNotSelected())
            return this;
        String[][] resume = {};
        resume = teacher.getResumePrint();
        page.pageResume(teacher.getName(), resume);
        readCommand();
        if (command == resume.length + 1) {
            return addResumeEntry();
        }
        if (command > 0 && command <= resume.length) {
            return deleteResumeEntry(command - 1);
        }
        return this;
    }

    private BasicController showTeacherDetails() throws UniversityFailure {
        if (teacherNotSelected())
            return this;

        String[] menu = { Messages.MENU_BACK, "Show courses", "Show resume",
                "Delete teacher" };
        page.pageTeacherDetailsHeader(university.getTitle());
        page.pagePersonDetails(
                teacher.getName(),
                teacher.getMobile(),
                teacher.getEmail()
                );
        page.pageTeacherDetailsMenu(menu);
        readCommand();
        if (command == 1) {
            return showCourses();
        }
        if (command == 2) {
            return showResume();
        }
        if (command == 3) {
            return deleteTeacher();
        }
        currentState = States.SHOW_TEACHERS_LIST;
        return this;
    }

    private BasicController showTeachersList() throws UniversityFailure {
        String[] teachers = university.teachers.getTeachers().toNamesList();
        page.pageShowTeachersList(university.getTitle(), teachers);
        readCommand();
        if (command > 0 && command <= teachers.length) {
            currentState = States.SHOW_TEACHER_DETAILS;
            try {
                teacher = university.teachers.getTeacherByIndex(command - 1);
            } catch (UniversityWarning e) {
                page.error("Failed to get a teacher information", e.getMessage());
            }
            return this;
        }
        if (command == teachers.length + 1) {
            return addNewTeacher();
        }
        return null;
    }

    private boolean teacherNotSelected() {
        if (teacher == null) {
            page.showWarning("Teacher is not selected");
            currentState = null;
            return true;
        }
        return false;
    }
}
